<?php
if ( count( get_included_files() ) == 1 ) die( '---' );

class Enregistrement
{
	var $enregistrementId;

	var $userId;
	
	var $competenceId;	
	
	var $note;
	
	var $evaluateur;
	
	var $date;

	var $tableEnregistrement;

	
    // Constructeur
    function Enregistrement() 
    {
    	$this->enregistrementId = -1;
    	$this->userId = '';
    	$this->competenceId = '';
    	$this->note = '';
	$this->evaluateur = '';
	$this->date = '';
	
	global $C2IPRATtables;    	
	$this->tableEnregistrement = $C2IPRATtables['C2IPRATenregistrement'];
    }
    
    // Load 
    function load($enregistrementId)
    {
 
    	$sql = "SELECT enregistrementId,userId,competenceId,note,evaluateur,UNIX_TIMESTAMP(date) as date
		FROM `".$this->tableEnregistrement."`
		WHERE enregistrementId = '".$enregistrementId."'";
		$data = claro_sql_query_get_single_row($sql);

	   if( !empty($data) )
	   {
	   	$this->enregistrementId     	= $data['enregistrementId'];
	   	$this->userId      		= $data['userId'];
	   	$this->competenceId      	= $data['competenceId'];
	   	$this->note			= $data['note'];
	   	$this->evaluateur      		= $data['evaluateur'];
	   	$this->date     		= $data['date'];
	   	
	   	return true;
	   }
	   else
	   return false;
    }
    
    
    // Save 
    function save()  
    {
    	if( $this->enregistrementId == -1 )
    	{
    		$sql = "INSERT INTO `".$this->tableEnregistrement."` 
			SET userId = '".$this->userId."',
			    competenceId = '".$this->competenceId."',
			    note = '".$this->note."',
			    evaluateur = '".$this->evaluateur."',";

		// NOW si pas de date
		if (!$this->date)
		$sql .= "date = NOW()";
		else
		$sql .= "date = '".$this->date."'";

			// execute the creation query and get id of inserted assignment
		    $insertedId = claro_sql_query_insert_id($sql);

		    if( $insertedId )
		    {
		    	$this->enregistrementId = (int) $insertedId;

		      return $this->enregistrementId;
		    }
		    else
		    {
		        return false;
		    }
    	}
    	else
    	{
    		$sql = "UPDATE `".$this->tableEnregistrement."` 
			SET userId = '".$this->userId."',
			    competenceId = '".$this->competenceId."',
			    note = '".$this->note."',
			    evaluateur = '".$this->evaluateur."',
			    date = '".$this->date."'
			WHERE enregistrementId = ".$this->enregistrementId;
			// execute and return main query
		    if( claro_sql_query($sql) )
		    {
		    	return $this->enregistrementId;
		    }
		    else
		    {
		    	return false;
		    }
    	}
    }
    
    	 function delete()
	 {
	 	$sql = "DELETE FROM `".$this->tableEnregistrement."` WHERE enregistrementId = ".$this->enregistrementId;
	 	if( claro_sql_query($sql) == false ) return false;
			
		$this->enregistrementId = -1;
		return true;
	 }
	 
	function chercheEnregistrement ($dernier=false)
	{
		$sql_search = "SELECT enregistrementId 
			       FROM `".$this->tableEnregistrement."`
			       WHERE competenceId = '".$this->competenceId."'
			       AND userId = '".$this->userId."'
			       ORDER BY date DESC";

		if ($dernier)
		{
			$sql_search .= " LIMIT 0,1";
			return claro_sql_query_get_single_value($sql_search);
		}
		else
		return claro_sql_query_fetch_all_rows($sql_search);
	}

	/**
     * get id
     *
     * @author Pierre Raynaud
     * @return int
     */
	function getEnregistrementId()
	{
		return (int) $this->enregistrementId;
	}
	
	/**
     * get userId
     *
     * @author Pierre Raynaud
     * @return string
     */
	function getUserId()
	{
		return (int) $this->userId;
	}
	
	/**
     * set userId
     *
     * @author Pierre Raynaud
     * @param $value
     */
	function setUserId($value)
	{
		$this->userId = (int) $value;
	}
	
	/**
     * get competenceId
     *
     * @author Pierre Raynaud
     * @return string
     */
	function getCompetenceId()
	{
		return $this->competenceId;
	}
	
	/**
     * set competenceId
     *
     * @author Pierre Raynaud
     * @param string $value
     */
	function setCompetenceId($value)
	{
		$this->competenceId = (int) $value;
	}
	
	/**
     * get note
     *
     * @author Pierre Raynaud
     * @return string
     */
	function getNote()
	{
		return $this->note;
	}
	
	/**
     * set setnote
     *
     * @author Pierre Raynaud
     * @param string $value
     */
	function setNote($value)
	{
		$this->note = (int) $value;
	}
	
	/**
     * get evaluateur
     *
     * @author Pierre Raynaud
     * @return string
     */
	function getEvaluateur()
	{
		return $this->evaluateur;
	}
	
	/**
     * set setevaluateur
     *
     * @author Pierre Raynaud
     * @param string $value
     */
	function setEvaluateur($value)
	{
		$this->evaluateur = (int) $value;
	}
	
	/**
     * get date
     *
     * @author Pierre Raynaud
     * @return int
     */
	function getDate()
	{
		return $this->date;
	}
	
	/**
     * set date
     *
     * @author Pierre Raynaud
     * @param int $value
     */
	function setDate($value)
	{
		$this->date = trim($value);
	}
}
?>
